package com.sushi.service.impl;

import com.sushi.repository.entity.OrderEntity;
import com.sushi.repository.mapper.OrderMapper;
import com.sushi.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sushi.response.ResponseBO;
import com.sushi.response.ResponsePageBO;
import com.sushi.response.OrderResp;
import com.sushi.request.OrderReq;
import com.sushi.request.OrderPageReq;
import com.sushi.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sushi.exception.BusinessException;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单 服务实现类
 * @date 2023-05-02
 * @time 17:15:28
 * @author Administrator
 */
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    

    /**
     * 新增
     */
    @Override
    public ResponseBO<OrderResp> add(OrderReq req) {
        OrderEntity entity = BeanUtil.convert(req, OrderEntity.class);
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("保存失败");
        }
        entity.insert();
        return ResponseUtil.ok(BeanUtil.convert(entity, OrderResp.class));
    }

    /**
     * 逻辑删除
     */
    @Override
    public ResponseBO<OrderResp> logicRemove(OrderReq req) {
        if (StringUtil.isBlank(req.getId())) {
            throw new BusinessException("id不能为空");
        }
        OrderEntity entity = new OrderEntity();
        entity.setId(req.getId());
        entity.setDeleted(1L);
        entity.updateById();
        return ResponseUtil.ok(BeanUtil.convert(entity, OrderResp.class));
    }

    /**
     * 更新
     */
    @Override
    public ResponseBO<OrderResp> updateByField(OrderReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        OrderEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            return ResponseUtil.error("信息不存在，无法更新");
        }
        if (StringUtil.isNotBlank(req.getStatus())) {
            entity.setStatus(req.getStatus());
        }
        if (StringUtil.isNotBlank(req.getSaleManId())) {
            entity.setSaleManId(req.getSaleManId());
        }
        if (StringUtil.isNotBlank(req.getSaleManName())) {
            entity.setSaleManName(req.getSaleManName());
        }
        if (StringUtil.isNotBlank(req.getPurchaseManId())) {
            entity.setPurchaseManId(req.getPurchaseManId());
        }
        if (StringUtil.isNotBlank(req.getPurchaseManName())) {
            entity.setPurchaseManName(req.getPurchaseManName());
        }
        if (ObjectUtil.isNotEmpty(req.getNum())) {
            entity.setNum(req.getNum());
        }
        if (ObjectUtil.isNotEmpty(req.getPerPrice())) {
            entity.setPerPrice(req.getPerPrice());
        }
//        if (ObjectUtil.isNotEmpty(req.getTotalPrice())) {
//            entity.setTotalPrice(req.getTotalPrice());
//        }
        if (StringUtil.isNotBlank(req.getOtherContent())) {
            entity.setOtherContent(req.getOtherContent());
        }

        entity.updateById();
        log.info("更新成功：{}", JSON.toJSONString(entity));
        return ResponseUtil.ok(BeanUtil.convert(entity, OrderResp.class));
    }

    /**
     * 列表
     */
    @Override
    public ResponsePageBO<OrderResp> listByCondition(OrderPageReq req) {
        if (ObjectUtil.isEmpty(req) || ObjectUtil.isEmpty(req.getPageNum()) || ObjectUtil.isEmpty(req.getPageSize())) {
            throw new BusinessException("必要参数不能为空");
        }
        LambdaQueryWrapper<OrderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderEntity::getDeleted, 0L);
        wrapper.eq(StringUtil.isNotBlank(req.getId()), OrderEntity::getId, req.getId());
        wrapper.eq(StringUtil.isNotBlank(req.getCommodityId()), OrderEntity::getCommodityId, req.getCommodityId());
//        wrapper.eq(StringUtil.isNotBlank(req.getStatus()), OrderEntity::getStatus, req.getStatus());
        wrapper.eq(StringUtil.isNotBlank(req.getSaleManId()), OrderEntity::getSaleManId, req.getSaleManId());
        wrapper.like(StringUtil.isNotBlank(req.getSaleManName()), OrderEntity::getSaleManName, req.getSaleManName());
        wrapper.eq(StringUtil.isNotBlank(req.getPurchaseManId()), OrderEntity::getPurchaseManId, req.getPurchaseManId());
        wrapper.like(StringUtil.isNotBlank(req.getPurchaseManName()), OrderEntity::getPurchaseManName, req.getPurchaseManName());
//        wrapper.eq(ObjectUtil.isNotEmpty(req.getNum()), OrderEntity::getNum, req.getNum());
//        wrapper.eq(ObjectUtil.isNotEmpty(req.getPerPrice()), OrderEntity::getPerPrice, req.getPerPrice());
//        wrapper.eq(ObjectUtil.isNotEmpty(req.getTotalPrice()), OrderEntity::getTotalPrice, req.getTotalPrice());
        wrapper.like(StringUtil.isNotBlank(req.getOtherContent()), OrderEntity::getOtherContent, req.getOtherContent());

        wrapper.orderByDesc(OrderEntity::getGmtCreateTime);

        Page<OrderEntity> page = page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        if (ObjectUtil.isEmpty(page) || CollectionUtil.isEmpty(page.getRecords())) {
            return ResponsePageUtil.ok(req, new ArrayList<>(), 0L);
        }
        List<OrderResp> convert = BeanUtil.convert(page.getRecords(), OrderResp.class);
        return ResponsePageUtil.ok(req, convert, page.getTotal());
    }

    /**
     * 详情
     */
    @Override
    public ResponseBO<OrderResp> detail(OrderReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        OrderEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("对象不存在");
        }
        return ResponseUtil.ok(BeanUtil.convert(entity, OrderResp.class));
    }

}